package cz.muni.fi.pa036.kiss.daoimpl;

import cz.muni.fi.pa036.kiss.Entity.Rezervace;
import cz.muni.fi.pa036.kiss.Entity.SluzbaKoleje;
import cz.muni.fi.pa036.kiss.Entity.Student;
import cz.muni.fi.pa036.kiss.dao.RezervaceDAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

public class RezervaceDAOImpl implements RezervaceDAO {

    @PersistenceContext
    private EntityManager em;

    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    @Override
    public void createRezervace(Rezervace rezervace) {
        if (rezervace == null) {
            throw new IllegalArgumentException("rezervace is null");
        }
        if (rezervace.getId() != null) {
            throw new IllegalArgumentException("rezervace.id is not null");
        }

        em.persist(rezervace);
    }

    @Override
    public Rezervace getRezervace(Long id) {
       if (id == null) {
            throw new IllegalArgumentException("null id");
        }
        Rezervace rezervace = em.find(Rezervace.class, id);
        return rezervace;
    }

    @Override
    public void updateRezervace(Rezervace rezervace) {
        if (rezervace == null) {
            throw new IllegalArgumentException("rezervace is null");
        }
        if (rezervace.getId() == null) {
            throw new IllegalArgumentException("rezervace.id is null");
        }

        em.merge(rezervace);
    }

    @Override
    public void removeRezervace(Rezervace rezervace) {
        if (rezervace == null) {
            throw new IllegalArgumentException("rezervace is null");
        }
        if (rezervace.getId() == null) {
            throw new IllegalArgumentException("rezervace.id is null");
        }
        Rezervace c = em.find(Rezervace.class, rezervace.getId());
        em.remove(c);
    }

    @Override
    public List<Rezervace> findAll() {
        Query query = em.createQuery("SELECT c FROM Rezervace c");
        List<Rezervace> results = query.getResultList();

        return results;
    }

    @Override
    public List<Rezervace> findByStudent(Student student) {
        if (student == null) {
            throw new IllegalArgumentException("null student");
        }
        Long student_id = student.getId();
        Query query = em.createQuery("SELECT c FROM Rezervace c WHERE c.student.id = student_id");
        List<Rezervace> results = query.getResultList();

        return results;
    }

    @Override
    public List<Rezervace> findBySluzba(SluzbaKoleje sluzbaKoleje) {
        if (sluzbaKoleje == null) {
            throw new IllegalArgumentException("null sluzbaKoleje");
        }
        Long sluzbaKoleje_id = sluzbaKoleje.getId();
        Query query = em.createQuery("SELECT c FROM Rezervace c WHERE c.sluzbaKoleje.id = sluzbaKoleje_id");
        List<Rezervace> results = query.getResultList();

        return results;
    }
}
